package com.qingpeng.pay.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qingpeng.pay.R;
import com.qingpeng.pay.adapter.TeamListAdapter;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.beans.TeamBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.utils.APP;
import com.qingpeng.pay.utils.MyDialog;
import com.qingpeng.pay.utils.StringUtils;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class TeamListActivity extends BaseActivity {
    @BindView(R.id.title)
    TitleBar title;
    @BindView(R.id.text_zhi)
    TextView textZhi;
    @BindView(R.id.text_max)
    TextView textMax;
    private List<TeamBean.DataProductBean.AgentlistProductBean> mList;
    private MyDialog mMyDialog;
    private EditText mNumber;
    private String mUser_id;
    private ArrayList<String> mDoubles;
    private int mSurimsinum;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_team_list;
    }

    @Override
    protected int getRootViewId() {
        return R.id.ll_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return R.id.recycler;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Call<TeamBean> agentlist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).agentlist();
        agentlist.enqueue(new Callback<TeamBean>() {
            @Override
            public void onResponse(Call<TeamBean> call, Response<TeamBean> response) {
                TeamBean body = response.body();
                if (body.getStatus() != 1) {
                    ToastUtils.showToast(TeamListActivity.this, body.getInfo());
                    return;
                }
                textMax.setText("总人数:"+body.getData().getTolnum());
                textZhi.setText("直推人数:"+body.getData().getPernum()+"");
                mSurimsinum = body.getData().getSurimsinum();
                mList = body.getData().getAgentlist();
                initAdapter();
            }

            @Override
            public void onFailure(Call<TeamBean> call, Throwable t) {
                ToastUtils.showToast(TeamListActivity.this, "网络请求错误,请稍后重试");
            }
        });
        title.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initAdapter() {
        mAdapter = new TeamListAdapter(R.layout.item_team);
        mAdapter.setNewData(mList);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.change_reta:
                        TeamBean.DataProductBean.AgentlistProductBean item = (TeamBean.DataProductBean.AgentlistProductBean) adapter.getItem(position);
                        mUser_id = item.getUser_id();
                        Call<NullBean> prosavelist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).prosavelist(mUser_id);
                        prosavelist.enqueue(new Callback<NullBean>() {
                            @Override
                            public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                                NullBean body = response.body();
                                if (body.getStatus()!=1){
                                    ToastUtils.showToast(TeamListActivity.this,body.getInfo());
                                    return;
                                }
                                List<Double> data = (List<Double>) body.getData();
                                showToastSingDialog(data);
                            }

                            @Override
                            public void onFailure(Call<NullBean> call, Throwable t) {
                                ToastUtils.showToast(TeamListActivity.this, "网络请求错误,请稍后重试");
                            }
                        });

                        break;
                    case R.id.text_fa:
                        TeamBean.DataProductBean.AgentlistProductBean item1 = (TeamBean.DataProductBean.AgentlistProductBean) adapter.getItem(position);
                        mUser_id = item1.getUser_id();
                        View view1 = getLayoutInflater().inflate(R.layout.dialog_invite, null);
                        mNumber = (EditText) view1.findViewById(R.id.ed_number);
                        TextView tip =(TextView) view1.findViewById(R.id.tip);
                        tip.setText("可用邀请码"+mSurimsinum);
                        view1.findViewById(R.id.text_ok).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Call<NullBean> issuecode = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).issuecode(mNumber.getText().toString(), mUser_id);
                                issuecode.enqueue(new Callback<NullBean>() {
                                    @Override
                                    public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                                        NullBean body = response.body();
                                        if (body.getStatus()!=1){
                                            ToastUtils.showToast(TeamListActivity.this,body.getInfo());
                                            return;
                                        }
                                        ToastUtils.showToast(TeamListActivity.this,body.getInfo());
                                        mMyDialog.dismiss();
                                    }

                                    @Override
                                    public void onFailure(Call<NullBean> call, Throwable t) {
                                        ToastUtils.showToast(TeamListActivity.this, "网络请求错误,请稍后重试");
                                    }
                                });
                            }
                        });
                        view1.findViewById(R.id.text_no).setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mMyDialog.dismiss();
                            }
                        });
                        mMyDialog = new MyDialog(TeamListActivity.this, 0, 0, view1, R.style.DialogTheme);
                        mMyDialog.setCancelable(true);
                        mMyDialog.show();
                        break;
                }
            }
        });
        super.initAdapter();
    }
    /**
     * 单选Dialog
     * @param data
     */
    private void showToastSingDialog(List<Double> data) {
        final String[] s = new String[1];
        Double[] doubles = data.toArray(new Double[data.size()]);
        final String[] strings = new String[data.size()];
        for (int i = 0; i <data.size() ; i++) {
            double a = doubles[i] * 1000;
            strings[i]=a/10+"%";
        }
        final AlertDialog.Builder singleChoiceDialog = new AlertDialog.Builder(TeamListActivity.this);
        singleChoiceDialog.setTitle("请设置被邀请人分润");
        singleChoiceDialog.setSingleChoiceItems(strings, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                s[0] =strings[which];
                ToastUtils.showToast(TeamListActivity.this,"更改费率为"+strings[which]);
            }
        });
        singleChoiceDialog.setPositiveButton("确认修改", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (!StringUtils.isEmpty(s[0])){
                    Call<NullBean> issuecode = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).changepro((Double.parseDouble(s[0].replace("%",""))/100)+"", mUser_id);
                    issuecode.enqueue(new Callback<NullBean>() {
                        @Override
                        public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                            NullBean body = response.body();
                            if (body.getStatus()!=1){
                                ToastUtils.showToast(TeamListActivity.this,body.getInfo());
                                return;
                            }
                            ToastUtils.showToast(TeamListActivity.this,body.getInfo());
                            singleChoiceDialog.create().cancel();
                            Call<TeamBean> agentlist = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).agentlist();
                            agentlist.enqueue(new Callback<TeamBean>() {
                                @Override
                                public void onResponse(Call<TeamBean> call, Response<TeamBean> response) {
                                    TeamBean body = response.body();
                                    if (body.getStatus() != 1) {
                                        ToastUtils.showToast(TeamListActivity.this, body.getInfo());
                                        return;
                                    }
                                    textMax.setText("总人数:"+body.getData().getTolnum());
                                    textZhi.setText("直推人数:"+body.getData().getPernum()+"");
                                    mSurimsinum = body.getData().getSurimsinum();
                                    mList = body.getData().getAgentlist();
                                    initAdapter();
                                }

                                @Override
                                public void onFailure(Call<TeamBean> call, Throwable t) {
                                    ToastUtils.showToast(TeamListActivity.this, "网络请求错误,请稍后重试");
                                }
                            });
                        }

                        @Override
                        public void onFailure(Call<NullBean> call, Throwable t) {
                            ToastUtils.showToast(TeamListActivity.this,"网络请求错误,请稍后重试");
                        }
                    });
                }
            }
        });
        singleChoiceDialog.create().show();
    }

}
